<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增客户端 版本更新')" />

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-version-add" name="form-version-add"
              enctype="multipart/form-data">

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">apk或ipa文件：</label>
                <div class="col-sm-8">
                    <input type="file" id="file" name="file" onchange="selectFile()" required/>
                </div>
            </div>

            <!--<div class="form-group">-->
                <!--<label class="col-sm-3 control-label">apk文件地址：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<textarea name="url" class="form-control"></textarea>-->
                <!--</div>-->
            <!--</div>-->

            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text" maxlength="20" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">版本号：</label>
                <div class="col-sm-8">
                    <input name="code" class="form-control" type="text" maxlength="10" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">描述：</label>
                <div class="col-sm-8">
                    <textarea name="description" class="form-control" maxlength="200" required></textarea>
                </div>
            </div>


            <!--<div class="form-group">    -->
                <!--<label class="col-sm-3 control-label">更新类型</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<select name="type" class="form-control m-b">-->
                        <!--<option value="">所有</option>-->
                    <!--</select>-->
                    <!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 代码生成请选择字典属性</span>-->
                <!--</div>-->
            <!--</div>-->


            <div class="form-group">
                <label class="col-sm-3 control-label is-required">更新类型：</label>
                <div class="col-sm-8">
                    <label th:each="type:${apptypes}" class="check-box">
                        <input name="type" type="checkbox" th:value="${type.dictValue}" th:text="${type.dictLabel}"
                               th:disabled="${type.status == '1'}">
                    </label>
                </div>
            </div>

            <!--  隐藏input，装树形插件的选中的ids  -->
            <input id="updateRegion" name="updateRegion" type="hidden" value=""/>

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">更新app的机构：</label>
                <div class="col-sm-8">
                    <div id="deptTrees" class="ztree"></div>
                </div>
            </div>


            <!--<div class="form-group">-->
                <!--<label class="col-sm-3 control-label is-required">更新app的机构：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<textarea name="updateRegion" class="form-control"></textarea>-->
                <!--</div>-->
            <!--</div>-->

            <!--<div class="form-group">    -->
                <!--<label class="col-sm-3 control-label">创建者id：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<input name="createId" class="form-control" type="text">-->
                <!--</div>-->
            <!--</div>-->

            <!--<div class="form-group">    -->
                <!--<label class="col-sm-3 control-label">更新者id：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<input name="updateId" class="form-control" type="text">-->
                <!--</div>-->
            <!--</div>-->

        </form>
    </div>

    <th:block th:include="include :: footer" />
    <!--  树形插件  -->
    <th:block th:include="include :: ztree-css" />
    <th:block th:include="include :: ztree-js" />
    <!--  多选下拉列表  -->
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
    <script type="text/javascript">
        var prefix = ctx + "yf/version"
        $("#form-version-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                // 选择的app更新类型
                var apptypes = $.form.selectCheckeds("type");
                if(apptypes == null || apptypes == '' ){
                    $.modal.alertWarning("请选择更新类型");
                    return;
                }
                // 选择的机构
                var deptIds = $.tree.getCheckedNodes();
                if(deptIds == null || deptIds == '' ){
                    $.modal.alertWarning("请选择更新app的机构");
                    return;
                }

                // var filePath = $("#file").val();
                // if( filePath != "" ){
                //     alert('apk不是空！');
                // }else{
                //     alert('apk是空！');
                // }

                // 赋值给隐藏的input
                $("#updateRegion").val(deptIds);
                // $.operate.save(prefix + "/add", $('#form-version-add').serialize());
                var formData = new FormData($("#form-version-add")[0]);
                var config = {
                    url: prefix + "/add",
                    type: "post",
                    data: formData,
                    processData: false,// 不加会报错
                    contentType: false,// 不加会报错
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        $.modal.disable();
                    },
                    success: function (result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.successCallback(result);
                    }
                };
                $.ajax(config);
            }
        }


        // 机构树形插件  var url = ctx + "system/dept/roleDeptTreeData?roleId=" + $("#roleId").val();
        $(function() {
            var url = ctx + "system/dept/treeData";
            var options = {
                id: "deptTrees",
                url: url,
                check: { enable: true, nocheckInherit: false, chkboxType: { "Y": "", "N": "" } },
                expandLevel: 3
            };
            $.tree.init(options);
        });

        // 选择apk文件
        function selectFile(){
            //获取点击的文本框
            var filePath = $("#file").val();
            if( filePath != "" ){
                //取文件后缀名
                var fileType = getFileType(filePath);
                // 判断上传的附件是否为apk
                if( fileType != "apk" && fileType != "ipa" ) {
                    $("#file").val("");
                    $.modal.alertWarning("请上传apk或ipa文件 ");
                }
            }
        }

        // 获取文件后缀名
        function getFileType(filePath) {
            var startIndex = filePath.lastIndexOf(".");
            if(startIndex != -1)
                return filePath.substring(startIndex + 1, filePath.length).toLowerCase();
            else return "";
        }

    </script>
</body>
</html>